<?php

namespace App\Kuafu\Order\Command;

use App\Kuafu\Order\Enum\OrderCacheEnum;
use App\Kuafu\Order\Enum\OrderCodeEnum;
use App\Kuafu\Order\Exceptions\OrderException;
use App\Kuafu\Order\Service\OrderService;
use App\Library\FeiShu\SendFs;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;

class ShopDayStatistics extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'order:shop-day-statistics';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '店铺日结单推送';

    /**
     * Create a new command instance.
     *
     * @return void//
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command
     *
     * @return int
     */
    public function handle()
    {
        $startTime = strtotime(date('Ymd'));
        $endTime   = strtotime(date('Ymd 23:00:00'));

        $shopId = 19; // 古美路店

        // 加锁
        $lockKey = OrderCacheEnum::getShopTradeLock($shopId);

        if (!Redis::setnx($lockKey, time())) {

            // 防止死锁
            $lock_time = Redis::get($lockKey);
            if (($lock_time + 5) < time()) {
                Redis::del($lockKey);
            } else {
                dump("您的请求太频繁啦，请稍后再试~");
                return;
            }
        }

        try {
            $list = OrderService::sCalcShopTrade($shopId, $startTime, $endTime);
        } catch (\Exception $e) {
            Redis::del($lockKey);
            throw new OrderException(OrderCodeEnum::ERR_ORDER_STATIC, '', $e);
        }

        $content = '日结单' . PHP_EOL;
        $content .= $list['shop']['shopCode'] . ' ' . $list['shop']['name'] . PHP_EOL;
        $content .= '时间范围:' . date('Y-m-d H:i', $startTime) . '至' . date('Y-m-d H:i', $endTime) . PHP_EOL;
        $content .= '---------------' . PHP_EOL;
        $content .= '【营业指标】' . PHP_EOL;
        $content .= '月度累计（元）' . '   ' . $list['source']['monthTotal'] / 100 . PHP_EOL;
        $content .= '订单量：' . $list['order']['count'] . PHP_EOL;
        $content .= '订单原价合计：' . $list['order']['originPrice'] / 100 . PHP_EOL;
        $content .= '订单优惠合计：' . $list['order']['discountPrice'] / 100 . PHP_EOL;
        $content .= '退款：' . $list['order']['refundPrice'] / 100 . PHP_EOL;
        $content .= '顾客应付合计：' . $list['order']['payPrice'] / 100 . PHP_EOL;
        $content .= '试吃：' . $list['order']['foretaste'] / 100 . PHP_EOL;
        $content .= '外卖平台服务费合计：' . $list['order']['waiMaiPlatformPrice'] / 100 . PHP_EOL;
        $content .= '订单实收合计：' . $list['order']['orderAccept'] / 100 . PHP_EOL;
        $content .= '---------------' . PHP_EOL;
        $content .= '【营业收款构成】' . PHP_EOL;
        $content .= '收款合计：' . $list['pay']['totalAccept']['count'] . '次' . '   ' . $list['pay']['totalAccept']['sum'] / 100 . PHP_EOL;
        $content .= '现金：' . $list['pay']['cash']['count'] . '次' . '   ' . $list['pay']['cash']['sum'] / 100 . PHP_EOL;
        $content .= '扫码支付：' . $list['pay']['scanPay']['count'] . '次' . '   ' . $list['pay']['scanPay']['sum'] / 100 . PHP_EOL;
        $content .= '    微信：' . $list['pay']['scanPay']['lists']['wxPay']['count'] . '次' . '   ' . $list['pay']['scanPay']['lists']['wxPay']['sum'] / 100 . PHP_EOL;
        $content .= '    支付宝：' . $list['pay']['scanPay']['lists']['aliPay']['count'] . '次' . '   ' . $list['pay']['scanPay']['lists']['aliPay']['sum'] / 100 . PHP_EOL;
        $content .= '美团/大众点评团购：' . $list['pay']['meiTuan']['count'] . '次' . '/' . $list['pay']['meiTuan']['orderNum'] . '张' . '   ' . $list['pay']['meiTuan']['sum'] / 100 . PHP_EOL;
        foreach ($list['pay']['meiTuan']['lists'] as $v) {
            $content .= "    {$v['name']}：" . $v['count'] . '次' . '/' . $v['orderNum'] . '张' . '   ' . $v['sum'] / 100 . PHP_EOL;
        }
        $content .= '外卖平台收款' . '    ' . $list['pay']['waiMai']['count'] . '次' . '    ' . $list['pay']['waiMai']['sum'] / 100 . PHP_EOL;
        $content .= '    美团外卖' . '    ' . $list['pay']['waiMai']['lists']['mt']['count'] . '次' . '    ' . $list['pay']['waiMai']['lists']['mt']['sum'] / 100 . PHP_EOL;
        $content .= '    饿了么' . '    ' . $list['pay']['waiMai']['lists']['elm']['count'] . '次' . '    ' . $list['pay']['waiMai']['lists']['elm']['sum'] / 100 . PHP_EOL;

        $content .= '---------------' . PHP_EOL;
        $content .= '【敏感操作】' . PHP_EOL;
        $content .= '试吃' . '   ' . $list['danger']['foretaste']['count'] . '单' . '   ' . $list['danger']['foretaste']['sum'] / 100 . PHP_EOL;
        $content .= '赠菜' . '   ' . $list['danger']['gift']['count'] . '单' . '   ' . $list['danger']['gift']['sum'] / 100 . PHP_EOL;
        $content .= '打折' . '   ' . $list['danger']['discount']['count'] . '单' . '   ' . $list['danger']['discount']['sum'] / 100 . PHP_EOL;
        $content .= '减免' . '   ' . $list['danger']['reduce']['count'] . '单' . '   ' . $list['danger']['reduce']['sum'] / 100 . PHP_EOL;
        $content .= '---------------' . PHP_EOL;
        $content .= '【活动详情】' . PHP_EOL;
        $content .= $list['activity']['inTotal']['name'] . '   ' . $list['activity']['inTotal']['count'] . '单' . '   '  . ($list['activity']['inTotal']['sum'] / 100). PHP_EOL;
        foreach ($list['activity']['inTotal']['lists'] as $v) {
            $content .= '   ' . $v['name'] . '   ' . $v['count'] . '单' . '   ' . ($v['sum'] / 100).PHP_EOL;
        }

        $content .= '---------------' . PHP_EOL;
        $content .= '【订单来源】' . PHP_EOL;
        $content .= '收银POS：' . '   ' . $list['source']['pos']['count'] . '单' . '   ' . $list['source']['pos']['sum'] / 100 . PHP_EOL;
        $content .= '扫码点餐：' . '   ' . $list['source']['scan']['count'] . '单' . '   ' . $list['source']['scan']['sum'] / 100 . PHP_EOL;
        $content .= '---------------' . PHP_EOL;
        $content .= '【消费指标】' . PHP_EOL;
        $content .= '单均折后（元）' . '   ' . $list['source']['average'] / 100 . PHP_EOL;
        $content .= '单均折前（元）' . '   ' . $list['source']['averageOriginal'] / 100 . PHP_EOL;
        $content .= '月度累计（元）' . '   ' . $list['source']['monthTotal'] / 100 . PHP_EOL;
        $content .= '---------------' . PHP_EOL;
        $content .= '【消费行为】' . PHP_EOL;
        $content .= '主扫' . '   ' . $list['pay']['action']['active']['count'] . '次' . '   ' . $list['pay']['action']['active']['sum'] / 100 . PHP_EOL;
        $content .= '被扫' . '   ' . $list['pay']['action']['passive']['count'] . '次' . '   ' . $list['pay']['action']['passive']['sum'] / 100 . PHP_EOL;

        SendFs::newRobot(SendFs::ROBOT_RJD)->setTitleContent('古美路店日结单推送', $content)->send();
    }
}
